// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129


const db = uniCloud.databaseForJQL();

module.exports = {
	_before: function() { // 通用预处理器

	},

	async createRecord(param) {

		const res = await db.collection('wz-record').where({
			user_id: param.user_id,
			category: param.category,
			full_date: param.full_date
		}).count();

		if (res.total > 0) {
			return await db.collection('wz-record').where({
				user_id: param.user_id,
				category: param.category,
				full_date: param.full_date
			}).update({
				data: param.data
			})
		} else {
			return await db.collection('wz-record').add({
				data: param.data,
				user_id: param.user_id,
				category: param.category,
				month_date: param.month_date,
				full_date: param.full_date
			})
		}
	},

	async addRecord(param) {
		await db.collection('wz-record').add({
			data: param.data,
			user_id: param.user_id,
			category: param.category,
			month_date: param.month_date,
			full_date: param.full_date
		})

	},
	async queryRecord(param) {
		const res = await db.collection('wz-record').where({
			user_id: param.user_id,
			category: param.category,
			month_date: param.month_date
		}).get()
		return res;
	},
	async queryRecordByDay(param) {
		const res = await db.collection('wz-record').where({
			user_id: param.user_id,
			category: param.category,
		}).orderBy('full_date desc').limit(param.days).get()
		return res;
	},
	async updateRecord(param) {
		await db.collection('wz-record').where({
			'user_id': param.user_id,
			category: param.category
		}).update({
			data: param.data
		})
	}
}